import Mock from 'mockjs'
import user from './user'

// get请求从config.url获取参数，post从config.body中获取参数
function param2Obj(url) {
    const search = url.split('?')[1]
    if (!search) {
        return {}
    }
    return JSON.parse(
        '{"' +
        decodeURIComponent(search)
            .replace(/"/g, '\\"')
            .replace(/&/g, '","')
            .replace(/=/g, '":"') +
        '"}'
    )
}
//blob:http://localhost:8080/0edb6b81-38d9-4e64-b7e2-eb33e49de19b
//https://i.328888.xyz/2023/02/23/xEWFy.png
let List = [{ name: '诺基亚', img: 'https://i.328888.xyz/2023/02/23/xEWFy.png', money: '98', stock: '500', shops: '数码旗舰店一店' }, { name: '手机模型', img: 'https://i.328888.xyz/2023/02/23/xElxZ.png', money: '18', stock: '1000', shops: '数码旗舰店一店' }, { name: '小灵通', img: 'https://i.328888.xyz/2023/02/23/xEboE.png', money: '128', stock: '800', shops: '数码旗舰店一店' }, { name: 'iPhone 12Pro', img: 'https://i.328888.xyz/2023/02/23/xEdMx.png', money: '8888', stock: '0', shops: '数码旗舰店三店' }, { name: 'iPhone 11Pro', img: 'https://i.328888.xyz/2023/02/23/xEGxa.png', money: '6888', stock: '1800', shops: '数码旗舰店三店' }, { name: 'iPhone 12', img: 'https://i.328888.xyz/2023/02/23/xzd3L.png', money: '4888', stock: '0', shops: '数码旗舰店三店' }, { name: '华为 P10', img: 'https://i.328888.xyz/2023/02/23/xE1tF.png', money: '6998', stock: '5000', shops: '数码旗舰店二店' }, { name: '华为 荣耀8', img: 'https://i.328888.xyz/2023/02/23/xzjzX.png', money: '2888', stock: '6400', shops: '数码旗舰店二店' }, { name: '华为 Mate9', img: 'https://i.328888.xyz/2023/02/23/xzobv.png', money: '6888', stock: '0', shops: '数码旗舰店二店' }, { name: '三星 Galaxy S', img: 'https://i.328888.xyz/2023/02/23/xzuQX.png', money: '5000', stock: '2000', shops: '数码旗舰店四店' }, { name: '三星 Galaxy N', img: 'https://i.328888.xyz/2023/02/23/xF0IQ.png', money: '6488', stock: '200', shops: '数码旗舰店四店' }, { name: '三星 Galaxy F', img: 'https://i.328888.xyz/2023/02/23/xFcud.png', money: '2088', stock: '0', shops: '数码旗舰店四店' }, { name: '魅族 Pro7', img: 'https://i.328888.xyz/2023/02/23/xFvB8.png', money: '5888', stock: '1200', shops: '数码旗舰店五店' }, { name: '魅族 M8', img: 'https://i.328888.xyz/2023/02/23/xFTZE.png', money: '588', stock: '500', shops: '数码旗舰店五店' }, { name: '魅族 MX4 Pro', img: 'https://i.328888.xyz/2023/02/23/xFaOC.png', money: '7188', stock: '80', shops: '数码旗舰店五店' }, { name: 'vivo X20', img: 'https://i.328888.xyz/2023/02/23/xFQaX.png', money: '4588', stock: '450', shops: '数码旗舰店六店' }, { name: 'vivo X20 Plus', img: 'https://i.328888.xyz/2023/02/23/xFuBJ.png', money: '3688', stock: '420', shops: '数码旗舰店六店' }, { name: 'vivo X9', img: 'https://i.328888.xyz/2023/02/23/xGVDA.png', money: '2168', stock: '780', shops: '数码旗舰店六店' }, { name: 'oppo R9s', img: 'https://i.328888.xyz/2023/02/23/xGw3k.png', money: '1988', stock: '800', shops: '数码旗舰店七店' }, { name: 'oppo A77', img: 'https://i.328888.xyz/2023/02/23/xGjzv.png', money: '5888', stock: '100', shops: '数码旗舰店七店' }, { name: 'oppo Reno6', img: 'https://i.328888.xyz/2023/02/23/xG3K8.png', money: '7888', stock: '0', shops: '数码旗舰店七店' }, { name: 'Redmi K50', img: 'https://i.328888.xyz/2023/02/23/xdeGp.png', money: '2088', stock: '4000', shops: '数码旗舰店八店' }, { name: 'Redmi Note11', img: 'https://i.328888.xyz/2023/02/23/xdvyF.png', money: '1568', stock: '6000', shops: '数码旗舰店八店' }, { name: 'Redmi 10A', img: 'https://i.328888.xyz/2023/02/23/xdM2C.png', money: '3688', stock: '0', shops: '数码旗舰店八店' }]


export default {
    /**
     * 获取列表
     * 要带参数 name, page, limt; name可以不填, page,limit有默认值。
     * @param name, page, limit
     * @return {{code: number, count: number, data: *[]}}
     */
    getGoodsList: config => {
        const { name } = param2Obj(config.url)
        const mockList = List.filter(user => {
            if (user.name.indexOf(name) === -1) return false
            return true
        })
        return {
            code: 20000,
            count: mockList.length,
            list: mockList,
            user: user
        }
    },
    /**
     * 增加用户
     * @param name, addr, age, birth, sex
     * @return {{code: number, data: {message: string}}}
     */
    createGoods: config => {
        const { name, money, img, stock, shops } = JSON.parse(config.body)
        console.log(JSON.parse(config.body))
        List.unshift({
            name: name,
            money: money,
            img: img,
            stock: stock,
            shops: shops
        })
        return {
            code: 20000,
            data: {
                message: '添加成功'
            }
        }
    },
    /**
     * 删除用户
     * @param id
     * @return {*}
     */
    deleteGoods: config => {
        const { name } = JSON.parse(config.body)
        if (!name) {
            return {
                code: -999,
                message: '参数不正确'
            }
        } else {
            List = List.filter(u => u.name !== name)
            return {
                code: 20000,
                message: '删除成功'
            }
        }
    },
    /**
     * 通过shop删除goods
     * @param id
     * @return {*}
     */
    shopDeleteGoods: config => {
        const { name } = JSON.parse(config.body)
        if (!name) {
            return {
                code: -999,
                message: '参数不正确'
            }
        } else {
            List = List.filter(u => u.shops !== name)
            console.log(List);
            return {
                code: 20000,
                message: '删除成功'
            }
        }
    },
    /**
     * 批量删除
     * @param config
     * @return {{code: number, data: {message: string}}}
     */
    batchremove: config => {
        let { ids } = param2Obj(config.url)
        ids = ids.split(',')
        List = List.filter(u => !ids.includes(u.id))
        return {
            code: 20000,
            data: {
                message: '批量删除成功'
            }
        }
    },
    /**
     * 修改用户
     * @param id, name, addr, age, birth, sex
     * @return {{code: number, data: {message: string}}}
     */
    updateGoods: config => {
        const { name, money, img, stock, shops } = JSON.parse(config.body)
        List.some(u => {
            if (u.name === name) {
                u.money = money
                u.img = img
                u.stock = stock
                u.shops = shops
                return true
            }
        })
        return {
            code: 20000,
            data: {
                message: '编辑成功'
            }
        }
    },
    /**
     * 店铺名修改时更新goods信息
     * @param id, name, addr, age, birth, sex
     * @return {{code: number, data: {message: string}}}
     */
    shopUpdateGoods: config => {
        console.log(config);
        const {oldName,newName} = JSON.parse(config.body)
        List.some(u => {
            if (u.shops === oldName) {
                u.shops = newName
            }
        })
        return {
            code: 20000,
            data: {
                message: '编辑成功'
            }
        }
    }
}